package com.llb666.shop.items.api.controller;

import com.alibaba.fastjson.JSON;
import com.llb666.common.core.web.response.BaseResponse;
import com.llb666.common.core.web.response.DataResponse;
import com.llb666.shop.cart.domain.entity.ShopCartBo;
import com.llb666.shop.cart.domain.service.ShopCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 购物车用户展示层.
 *
 * @author llb666
 */
@Tag(name = "购物车接口相关的api")
@Slf4j
@RestController
@RequestMapping("shopcart")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopCartController {

    private final ShopCartService shopCartService;

    @Operation(summary = "添加商品到购物车")
    @PostMapping("/add")
    public BaseResponse add(@Parameter(description = "用户Id") @RequestParam String userId,
                            @RequestBody ShopCartBo shopCartBo) {
        if (StringUtils.isBlank(userId)) {
            return DataResponse.errorMsg("");
        }
        log.info(JSON.toJSONString(shopCartBo));
        shopCartService.add(userId, shopCartBo);
        return BaseResponse.ok();
    }

    @Operation(summary = "从购物车中删除商品")
    @PostMapping("/del")
    public BaseResponse del(@Parameter(description = "用户Id") @RequestParam String userId,
                            @Parameter(description = "商品规格Id") @RequestParam String itemSpecId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return DataResponse.errorMsg("参数不能为空");
        }
        shopCartService.del(userId, itemSpecId);
        return BaseResponse.ok();
    }

}
